<template>
  <div class="flex h-full">
    <div class="flex flex-col flex-1 overflow-hidden">
      <SearchContainer search-text="搜索" @search="getData()" @reset="onReset">
        <div class="flex justify-between">
          <a-form ref="formRef" class="form-style-flex" :model="searchData" :label-col="{ style: { width: '80px' } }">
            <a-form-item label="" name="dateRange" class="w-52 mr-2">
              <SelectArea use-cache set-cache @change="onChangeArea" />
            </a-form-item>
            <a-form-item label="发放日期" name="dateRange" class="w-80 mr-4">
              <a-range-picker v-model:value="searchData['dateRange']" format="YYYY-MM-DD" value-format="YYYY-MM-DD" :allow-clear="false" @change="getData()" />
            </a-form-item>
            <a-form-item label="" name="dateRange" class="w-64 mr-4">
              <a-input v-model:value="searchData.pyjm" placeholder="床位号/姓名/助记码/住院号" allow-clear />
            </a-form-item>
            <a-form-item label="" name="dateRange" class="w-64 mr-4">
              <a-input v-model:value="searchData.xmmc" placeholder="项目名称/助记码" allow-clear />
            </a-form-item>
          </a-form>
          <a-form class="flex">
            <a-form-item class="w-64" label="审核人">
              <a-select v-model:value="reviewer" :options="pharmacistSelectData" placeholder="请选择" @change="setLocalStorageEvent" />
            </a-form-item>
          </a-form>
        </div>
      </SearchContainer>
      <a-card class="w-full flex-1 mt-2">
        <BaseTable :loading="loading" column-code="000560" :data-source="tableData" row-key="bh" fill :pagination="pagination">
          <template #bodyCell="{ column, record, index }">
            <template v-if="column.dataIndex === 'xh'">
              <span>{{ index + 1 }}</span>
            </template>
            <template v-if="column.dataIndex === 'mc'">
              <MedicineInfo :value="{ mc: record.mc, gg: record.gg, cj: record.cj }" />
            </template>
            <template v-if="column.dataIndex === 'brName'">
              <span>{{ record.brName }}</span>
              <span class="ml-4">{{ record.brXb }}</span>
              <span class="ml-4">{{ record.brAge }}</span>
            </template>
            <template v-if="column.dataIndex === 'sl'"> {{ record.sl }}{{ record.dw }} </template>
            <template v-if="column.dataIndex === 'action'">
              <BaseButton confirm-text="是否确认退回?" type="link" size="small" @click="onReturn(record)"> 退回 </BaseButton>
            </template>
          </template>
        </BaseTable>
      </a-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { post } from '@/utils/request'
import { isHave } from '@/utils/tools'
import useUserStore from '@/store/user'

const { empId } = useUserStore()
const formRef = ref(null)
const pharmacistSelectData = ref([])
const reviewer = ref(undefined) //审核人
const searchData = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  pyjm: '',
  wardId: '',
  xmmc: ''
})
//获取
const { loading, tableData, getData, pagination } = useTable({
  url: '/inp/zyyf/getInpatientSuppliesReturned',
  immediate: false,
  formatParams: () => {
    return {
      stdate: searchData.value?.dateRange[0],
      eddate: searchData.value?.dateRange[1],
      pyjm: searchData.value.pyjm,
      xmmc: searchData.value.xmmc,
      wardId: searchData.value.wardId
    }
  }
  // onSuccess: () => {
  //   tableData.value.forEach((item, index) => {
  //     item.idx = index
  //   })
  // }
})

//获取审核人下拉（类别为药剂师）
const getSelectPharmacist = async () => {
  const { code, data } = await post('/operation/opex/getEmpInfoList', {
    emplbid: '5',
    yxzt: 0,
    pageNum: 1,
    pageSize: 9999,
    notEmpIdList: [empId] // 当前登录但id
  })
  if (code !== 0) return
  pharmacistSelectData.value = data.map((item) => {
    return {
      label: item.empName,
      value: item.empId
    }
  })
  const reviewerValue = JSON.parse(window.localStorage.getItem('grantReviewer'))
  if (reviewerValue && reviewerValue.time && reviewerValue.value) {
    //保存24小时
    if (dayjs(dayjs().format()).diff(reviewerValue.time, 'hour') >= 0 && dayjs(dayjs().format()).diff(reviewerValue.time, 'hour') < 24) {
      if (isHave(pharmacistSelectData.value, reviewerValue.value)) reviewer.value = reviewerValue.value
    }
  }
}
const onReturn = async (record) => {
  if (!reviewer.value) return message.error('请选择审核人')
  const res = await post('/inp/zyyf/doSaveInpatientSuppliesReturned', {
    inplsh: record.inplsh,
    bh: record.bh,
    sl: record.sl,
    yzId: [record.id],
    shr: reviewer.value
  })
  if (res.code !== 0) return message.error(res.msg || '操作失败')
  message.success('退回成功')
  getData()
  onReset()
}

const onChangeArea = (val) => {
  searchData.value.wardId = val
  getData()
}
const onReset = () => {
  formRef.value.resetFields()
}
const setLocalStorageEvent = () => {
  window.localStorage.setItem(
    'grantReviewer',
    JSON.stringify({
      time: dayjs().format(),
      value: reviewer.value
    })
  )
}
onMounted(() => {
  getSelectPharmacist()
})
</script>

<style lang="less" scoped>
:deep(.ant-table-row-expand-icon) {
  display: none;
}
</style>
